//
//  HJAccount.m
//  12期传智微博HJ
//
//  Created by mac on 15-2-3.
//  Copyright (c) 2015年 jun. All rights reserved.
//

#import "HJAccount.h"
#import <objc/runtime.h>
#import <objc/message.h>

#define HJAccountFileName @"account.data"

@implementation HJAccount
MJCodingImplementation

- (BOOL)save
{
    //1.获取沙盒路径
    NSString *accountPath = [HJAccountFileName appendDocumentDir];
    //2.生成真正的过期时间
    NSDate *now = [NSDate date];
    
    self.expires_time = [now dateByAddingTimeInterval:[self.expires_in doubleValue]];
    //把自己存储起来
    return [NSKeyedArchiver archiveRootObject:self toFile:accountPath];
}
+ (instancetype)accountFromSandbox
{
    //获取沙盒路径
    NSString *accountPath = [HJAccountFileName appendDocumentDir];
    //取出存储的对象
    HJAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath];
    //判断授权是否过期
    NSDate *now = [NSDate date];
    
    if ([now compare:account.expires_time] != NSOrderedAscending) {
        return nil;
    }
    //返回模型对象
    return account;
    
}
@end
